# -*- coding: utf-8 -*-
# @Time    : 2024/2/1 21:49
# @Author  : micah
# @File    : 12.页面等待.py
# @Software: PyCharm

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  # 等待的包
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()

# 创建等待对象
wait_obj = WebDriverWait(browser, 10)
browser.get('https://www.jd.com')

# 等待某个标签出现, 如果在规定时间内没有加载完成则抛出异常
# presence_of_element_located: 等待指定的单个标签加载成功: 查询数据需要使用元组的方式进行传递
search_input = wait_obj.until(EC.presence_of_element_located((By.ID, 'key')))
print(search_input)

search_input.send_keys('口红')

search_button = wait_obj.until(EC.presence_of_element_located((By.XPATH, "//button[@class='button']")))
search_button.click()
time.sleep(2)
browser.close()

"""
页面等待使用的方式为:presence_of_element_located
    presence_of_element_located需要传递一个元组: 元组中的第一个元素为查询方式, 第二个元素为查询规则
"""
